home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / net / readline-2.0mg.tar.gz / readline-2.0mg.tar / readline-2.0mg / tilde.c < prev    next >
C/C++ Source or Header  |  1995-02-12  |  10KB  |  391 lines

  1. /* tilde.c -- Tilde expansion code (~/foo := $HOME/foo). */
  2.  
  3. /* Copyright (C) 1988,1989 Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU Readline, a library for reading lines
  6.    of text with interactive input and history editing.
  7.  
  8.    Readline is free software; you can redistribute it and/or modify it
  9.    under the terms of the GNU General Public License as published by the
  10.    Free Software Foundation; either version 1, or (at your option) any
  11.    later version.
  12.  
  13.    Readline is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with Readline; see the file COPYING.  If not, write to the Free
  20.    Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  21.  
  22. #if defined (HAVE_CONFIG_H)
  23. #  include "config.h"
  24. #endif
  25.  
  26. #include "memalloc.h"
  27.  
  28. #if defined (HAVE_STRING_H)
  29. #  include <string.h>
  30. #else /* !HAVE_STRING_H */
  31. #  include <strings.h>
  32. #endif /* !HAVE_STRING_H */  
  33.  
  34. #if defined (HAVE_STDLIB_H)
  35. #  include <stdlib.h>
  36. #else
  37. #  include "ansi_stdlib.h"
  38. #endif /* HAVE_STDLIB_H */
  39.  
  40. #include "tilde.h"
  41. #include <pwd.h>
  42.  
  43. #if defined (USG) && !defined (HAVE_GETPW_DECLS)
  44. extern struct passwd *getpwuid (), *getpwnam ();
  45. #endif /* USG && !defined (HAVE_GETPW_DECLS) */
  46.  
  47. #if !defined (savestring)
  48. extern char *xmalloc ();
  49. #  ifndef strcpy
  50. extern char *strcpy ();
  51. #  endif
  52. #define savestring(x) strcpy (xmalloc (1 + strlen (x)), (x))
  53. #endif /* !savestring */
  54.  
  55. #if !defined (NULL)
  56. #  if defined (__STDC__)
  57. #    define NULL ((void *) 0)
  58. #  else
  59. #    define NULL 0x0
  60. #  endif /* !__STDC__ */
  61. #endif /* !NULL */
  62.  
  63. #if defined (TEST) || defined (STATIC_MALLOC)
  64. static char *xmalloc (), *xrealloc ();
  65. #else
  66. extern char *xmalloc (), *xrealloc ();
  67. #endif /* TEST || STATIC_MALLOC */
  68.  
  69. /* The default value of tilde_additional_prefixes.  This is set to
  70.    whitespace preceding a tilde so that simple programs which do not
  71.    perform any word separation get desired behaviour. */
  72. static char *default_prefixes[] =
  73.   { " ~", "\t~", (char *)NULL };
  74.  
  75. /* The default value of tilde_additional_suffixes.  This is set to
  76.    whitespace or newline so that simple programs which do not
  77.    perform any word separation get desired behaviour. */
  78. static char *default_suffixes[] =
  79.   { " ", "\n", (char *)NULL };
  80.  
  81. /* If non-null, this contains the address of a function to call if the
  82.    standard meaning for expanding a tilde fails.  The function is called
  83.    with the text (sans tilde, as in "foo"), and returns a malloc()'ed string
  84.    which is the expansion, or a NULL pointer if there is no expansion. */
  85. CPFunction *tilde_expansion_failure_hook = (CPFunction *)NULL;
  86.  
  87. /* When non-null, this is a NULL terminated array of strings which
  88.    are duplicates for a tilde prefix.  Bash uses this to expand
  89.    `=~' and `:~'. */
  90. char **tilde_additional_prefixes = default_prefixes;
  91.  
  92. /* When non-null, this is a NULL terminated array of strings which match
  93.    the end of a username, instead of just "/".  Bash sets this to
  94.    `:' and `=~'. */
  95. char **tilde_additional_suffixes = default_suffixes;
  96.  
  97. /* Find the start of a tilde expansion in STRING, and return the index of
  98.    the tilde which starts the expansion.  Place the length of the text
  99.    which identified this tilde starter in LEN, excluding the tilde itself. */
  100. static int
  101. tilde_find_prefix (string, len)
  102.      char *string;
  103.      int *len;
  104. {
  105.   register int i, j, string_len;
  106.   register char **prefixes = tilde_additional_prefixes;
  107.  
  108.   string_len = strlen (string);
  109.   *len = 0;
  110.  
  111.   if (!*string || *string == '~')
  112.     return (0);
  113.  
  114.   if (prefixes)
  115.     {
  116.       for (i = 0; i < string_len; i++)
  117.     {
  118.       for (j = 0; prefixes[j]; j++)
  119.         {
  120.           if (strncmp (string + i, prefixes[j], strlen (prefixes[j])) == 0)
  121.         {
  122.           *len = strlen (prefixes[j]) - 1;
  123.           return (i + *len);
  124.         }
  125.         }
  126.     }
  127.     }
  128.   return (string_len);
  129. }
  130.  
  131. /* Find the end of a tilde expansion in STRING, and return the index of
  132.    the character which ends the tilde definition.  */
  133. static int
  134. tilde_find_suffix (string)
  135.      char *string;
  136. {
  137.   register int i, j, string_len;
  138.   register char **suffixes = tilde_additional_suffixes;
  139.  
  140.   string_len = strlen (string);
  141.  
  142.   for (i = 0; i < string_len; i++)
  143.     {
  144.       if (string[i] == '/' || !string[i])
  145.     break;
  146.  
  147.       for (j = 0; suffixes && suffixes[j]; j++)
  148.     {
  149.       if (strncmp (string + i, suffixes[j], strlen (suffixes[j])) == 0)
  150.         return (i);
  151.     }
  152.     }
  153.   return (i);
  154. }
  155.  
  156. /* Return a new string which is the result of tilde expanding STRING. */
  157. char *
  158. tilde_expand (string)
  159.      char *string;
  160. {
  161.   char *result, *tilde_expand_word ();
  162.   int result_size, result_index;
  163.  
  164.   result_size = result_index = 0;
  165.   result = (char *)NULL;
  166.  
  167.   /* Scan through STRING expanding tildes as we come to them. */
  168.   while (1)
  169.     {
  170.       register int start, end;
  171.       char *tilde_word, *expansion;
  172.       int len;
  173.  
  174.       /* Make START point to the tilde which starts the expansion. */
  175.       start = tilde_find_prefix (string, &len);
  176.  
  177.       /* Copy the skipped text into the result. */
  178.       if ((result_index + start + 1) > result_size)
  179.     result = (char *)xrealloc (result, 1 + (result_size += (start + 20)));
  180.  
  181.       strncpy (result + result_index, string, start);
  182.       result_index += start;
  183.  
  184.       /* Advance STRING to the starting tilde. */
  185.       string += start;
  186.  
  187.       /* Make END be the index of one after the last character of the
  188.      username. */
  189.       end = tilde_find_suffix (string);
  190.  
  191.       /* If both START and END are zero, we are all done. */
  192.       if (!start && !end)
  193.     break;
  194.  
  195.       /* Expand the entire tilde word, and copy it into RESULT. */
  196.       tilde_word = (char *)xmalloc (1 + end);
  197.       strncpy (tilde_word, string, end);
  198.       tilde_word[end] = '\0';
  199.       string += end;
  200.  
  201.       expansion = tilde_expand_word (tilde_word);
  202.       free (tilde_word);
  203.  
  204.       len = strlen (expansion);
  205.       if ((result_index + len + 1) > result_size)
  206.     result = (char *)xrealloc (result, 1 + (result_size += (len + 20)));
  207.  
  208.       strcpy (result + result_index, expansion);
  209.       result_index += len;
  210.       free (expansion);
  211.     }
  212.  
  213.   result[result_index] = '\0';
  214.  
  215.   return (result);
  216. }
  217.  
  218. /* Do the work of tilde expansion on FILENAME.  FILENAME starts with a
  219.    tilde.  If there is no expansion, call tilde_expansion_failure_hook. */
  220. char *
  221. tilde_expand_word (filename)
  222.      char *filename;
  223. {
  224.   char *dirname;
  225.  
  226.   dirname = filename ? savestring (filename) : (char *)NULL;
  227.  
  228.   if (dirname && *dirname == '~')
  229.     {
  230.       char *temp_name;
  231.       if (!dirname[1] || dirname[1] == '/')
  232.     {
  233.       /* Prepend $HOME to the rest of the string. */
  234.       char *temp_home = (char *)getenv ("HOME");
  235.  
  236.       /* If there is no HOME variable, look up the directory in
  237.          the password database. */
  238.       if (!temp_home)
  239.         {
  240.           struct passwd *entry;
  241.  
  242.           entry = getpwuid (getuid ());
  243.           if (entry)
  244.         temp_home = entry->pw_dir;
  245.         }
  246.  
  247.       temp_name = xmalloc (1 + strlen (&dirname[1])
  248.                  + (temp_home ? strlen (temp_home) : 0));
  249.       temp_name[0] = '\0';
  250.       if (temp_home)
  251.         strcpy (temp_name, temp_home);
  252.       strcat (temp_name, dirname + 1);
  253.       free (dirname);
  254.       dirname = temp_name;
  255.     }
  256.       else
  257.     {
  258.       char u_name[257];
  259.       struct passwd *user_entry;
  260.       char *username;
  261.       int i, c;
  262.  
  263.       username = u_name;
  264.       for (i = 1; c = dirname[i]; i++)
  265.         {
  266.           if (c == '/')
  267.         break;
  268.           else
  269.         username[i - 1] = c;
  270.         }
  271.       username[i - 1] = '\0';
  272.  
  273.       if (!(user_entry = getpwnam (username)))
  274.         {
  275.           /* If the calling program has a special syntax for
  276.          expanding tildes, and we couldn't find a standard
  277.          expansion, then let them try. */
  278.           if (tilde_expansion_failure_hook)
  279.         {
  280.           char *expansion;
  281.  
  282.           expansion = (*tilde_expansion_failure_hook) (username);
  283.  
  284.           if (expansion)
  285.             {
  286.               temp_name = xmalloc (1 + strlen (expansion)
  287.                           + strlen (&dirname[i]));
  288.               strcpy (temp_name, expansion);
  289.               strcat (temp_name, &dirname[i]);
  290.               free (expansion);
  291.               free (dirname);
  292.               dirname = temp_name;
  293.             }
  294.         }
  295.           /* We shouldn't report errors. */
  296.         }
  297.       else
  298.         {
  299.           temp_name = xmalloc (1 + strlen (user_entry->pw_dir)
  300.                      + strlen (&dirname[i]));
  301.           strcpy (temp_name, user_entry->pw_dir);
  302.           strcat (temp_name, &dirname[i]);
  303.           free (dirname);
  304.           dirname = temp_name;
  305.         }
  306.         endpwent ();
  307.     }
  308.     }
  309.   return (dirname);
  310. }
  311.  
  312.  
  313. #if defined (TEST)
  314. #undef NULL
  315. #include <stdio.h>
  316.  
  317. main (argc, argv)
  318.      int argc;
  319.      char **argv;
  320. {
  321.   char *result, line[512];
  322.   int done = 0;
  323.  
  324.   while (!done)
  325.     {
  326.       printf ("~expand: ");
  327.       fflush (stdout);
  328.  
  329.       if (!gets (line))
  330.     strcpy (line, "done");
  331.  
  332.       if ((strcmp (line, "done") == 0) ||
  333.       (strcmp (line, "quit") == 0) ||
  334.       (strcmp (line, "exit") == 0))
  335.     {
  336.       done = 1;
  337.       break;
  338.     }
  339.  
  340.       result = tilde_expand (line);
  341.       printf ("  --> %s\n", result);
  342.       free (result);
  343.     }
  344.   exit (0);
  345. }
  346.  
  347. static void memory_error_and_abort ();
  348.  
  349. static char *
  350. xmalloc (bytes)
  351.      int bytes;
  352. {
  353.   char *temp = (char *)malloc (bytes);
  354.  
  355.   if (!temp)
  356.     memory_error_and_abort ();
  357.   return (temp);
  358. }
  359.  
  360. static char *
  361. xrealloc (pointer, bytes)
  362.      char *pointer;
  363.      int bytes;
  364. {
  365.   char *temp;
  366.  
  367.   if (!pointer)
  368.     temp = (char *)malloc (bytes);
  369.   else
  370.     temp = (char *)realloc (pointer, bytes);
  371.  
  372.   if (!temp)
  373.     memory_error_and_abort ();
  374.  
  375.   return (temp);
  376. }
  377.  
  378. static void
  379. memory_error_and_abort ()
  380. {
  381.   fprintf (stderr, "readline: Out of virtual memory!\n");
  382.   abort ();
  383. }
  384.  
  385. /*
  386.  * Local variables:
  387.  * compile-command: "gcc -g -DTEST -o tilde tilde.c"
  388.  * end:
  389.  */
  390. #endif /* TEST */
  391.